package com.autotest0209;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.util.Set;

/**
 * @author krystal
 * @date 2023/3/1 13:03
 */
public class autotest {
    ChromeDriver driver=new ChromeDriver();
    public void baseConTrol() throws InterruptedException {
        Thread.sleep(3000);
        driver.get("https://www.baidu.com");
        Thread.sleep(3000);
        //具体操作-输入文本
//        WebElement ele=driver.findElement(By.cssSelector("#kw"));
//        ele.sendKeys("baekhyun yyds!");
        //driver.findElement(By.cssSelector("#kw")).sendKeys("baekhyun yyds");
        // 点击
        //driver.findElement(By.cssSelector("#su")).click();

        //提交
        //driver.findElement(By.cssSelector("#su")).submit();

        //清除
//        driver.findElement(By.cssSelector("#kw")).sendKeys("baekhyun yyds");
//        Thread.sleep(2000);
//        driver.findElement(By.cssSelector("#kw")).clear();
//        driver.findElement(By.cssSelector("#kw")).sendKeys("baekhyun");

        //获取文本
        //String text=driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(2) > a > span.title-content-title")).getText();
        //System.out.println("获取到的文本："+text);

        //获取标题和url
        System.out.println(driver.getTitle());
        System.out.println(driver.getCurrentUrl());
        driver.findElement(By.cssSelector("#kw")).sendKeys("边伯贤");
        driver.findElement(By.cssSelector("#su")).click();
        System.out.println(driver.getTitle());
        System.out.println(driver.getCurrentUrl());
        Thread.sleep(3000);
        driver.quit();
    }

    public void windowControl() throws InterruptedException, IOException {
        driver.get("https://www.baidu.com");
        Thread.sleep(3000);
//        //窗口最大化
//        driver.manage().window().maximize();
//        Thread.sleep(3000);
//        //窗口最小化
//        driver.manage().window().minimize();
//        Thread.sleep(3000);
//        //全屏
//        driver.manage().window().fullscreen();
//        Thread.sleep(3000);
//        //手动设置窗口大小
//        driver.manage().window().setSize(new Dimension(1024,512));

        //窗口的切换
//        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
//        //
//        String curhandel=driver.getWindowHandle();
//        System.out.println(curhandel);
//        //先获取所有标签的句柄
//        Set<String> handels=driver.getWindowHandles();
//        for (String handel:handels){
//            if (handel!=curhandel){
//                driver.switchTo().window(handel);
//            }
//        }
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#homeSearchForm > span.s_btn_wr > input")).click();
//        Thread.sleep(3000);

        driver.findElement(By.cssSelector("#kw")).sendKeys("边伯贤");
        driver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        //屏幕截图（保存现场）
        File srcfile=driver.getScreenshotAs(OutputType.FILE);
        //把屏幕截图好的文件放到指定的路径下
        String filename="my.png";
        FileUtils.copyFile(srcfile,new File(filename));
        driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div.cos-row.row-text_Johh7.row_5y9Az > div > a > div > p > span > span"));
        driver.quit();
    }

    //博客登录（自动）
    public void TestBlogLogin() throws InterruptedException {
        driver.get("http://101.42.46.10:8080/blogSystem/login.html");
        Thread.sleep(3000);
        //输入账户和密码
        //点击登录按钮
        driver.findElement(By.cssSelector("#username")).sendKeys("啵啵");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#login-btn")).click();
        Thread.sleep(3000);
        driver.quit();
    }
}
